﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.Security;
using System.Windows.Controls;
using System.Globalization;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using SPS.ImageAttachementFieldType.WebControls;
using SPS.ImageAttachementFieldType.System.Windows.Controls;

namespace SPS.ImageAttachementFieldType
{
    public class ImageAttachementField : SPFieldUrl
    {
        public ImageAttachementField(SPFieldCollection fields, string fieldName)
            : base(fields, fieldName)
        {
            base.DisplayFormat = SPUrlFieldFormatType.Hyperlink;
        }

        public ImageAttachementField(SPFieldCollection fields, string typeName, string displayName)
            : base(fields, typeName, displayName)
        {
            base.DisplayFormat = SPUrlFieldFormatType.Hyperlink;
        }

        public string UrlFieldHref { get; set; }

        public override BaseFieldControl FieldRenderingControl
        {
            [SharePointPermission(SecurityAction.LinkDemand, ObjectModel = true)]
            get
            {
                BaseFieldControl fieldControl = new ImageAttachementFieldControl();
                fieldControl.FieldName = this.InternalName;
                return fieldControl;
            }
        }

        public override string GetValidatedString(object value)
        {
            //fix done to solve an exception in case th coulmn is required
            if ((this.Required == true) && ((value == null) || string.IsNullOrEmpty(value.ToString())))
            {
                throw new SPFieldValidationException(this.Title + " must have a value.");
            }
            else
            {
                ImageHyperlinkValidationRule rule = new ImageHyperlinkValidationRule();
                ValidationResult result = rule.Validate(value, CultureInfo.InvariantCulture);
                if (!result.IsValid)
                    throw new SPFieldValidationException((String)result.ErrorContent);
                else
                {
                    return base.GetValidatedString(value);
                }
            }
        }

        public override void ParseAndSetValue(SPListItem item, string value)
        {
            if (item == null)
            {
                throw new ArgumentNullException("item");
            }
            this.CheckRequiredValue(value);
            if (string.IsNullOrEmpty(value))
            {
                item[this.InternalName] = null;
            }
            else
            {
                item[this.InternalName] = value;
            }
        }

        internal virtual void CheckRequiredValue(string value)
        {
            if (string.IsNullOrEmpty(value) && this.Required)
            {
                throw new SPException(SPResource.GetString("MissingRequiredField", new object[0]));
            }
        }
    }
}
